package com.docmgmt.mvp.mapper;

import com.docmgmt.mvp.entity.UserFavoriteEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 用户收藏表 Mapper 接口
 * </p>
 *
 * @author Dora
 */
@Mapper
public interface UserFavoriteMapper extends BaseMapper<UserFavoriteEntity> {

    /**
     * Query favorite record by user, resource type and resource ID
     *
     * @param userId       User ID
     * @param resourceType Resource type
     * @param resourceId   Resource ID
     * @return Favorite entity
     */
    @Select("SELECT * FROM doc_user_favorite WHERE user_id = #{userId} " +
            "AND resource_type = #{resourceType} AND resource_id = #{resourceId} LIMIT 1")
    UserFavoriteEntity selectByUserAndResource(@Param("userId") Long userId,
                                                @Param("resourceType") String resourceType,
                                                @Param("resourceId") Long resourceId);

    /**
     * Query user favorites by type
     *
     * @param userId       User ID
     * @param resourceType Resource type
     * @return Favorite list
     */
    @Select("SELECT * FROM doc_user_favorite WHERE user_id = #{userId} " +
            "AND resource_type = #{resourceType} ORDER BY created_at DESC")
    List<UserFavoriteEntity> listByUserAndType(@Param("userId") Long userId,
                                                @Param("resourceType") String resourceType);

    /**
     * Delete favorite by user and resource
     *
     * @param userId       User ID
     * @param resourceType Resource type
     * @param resourceId   Resource ID
     * @return Delete count
     */
    @Delete("DELETE FROM doc_user_favorite WHERE user_id = #{userId} " +
            "AND resource_type = #{resourceType} AND resource_id = #{resourceId}")
    int deleteByUserAndResource(@Param("userId") Long userId,
                                 @Param("resourceType") String resourceType,
                                 @Param("resourceId") Long resourceId);
}
